package vip.xiaonuo.smzq.modular.lifecycle.controller.entity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import java.util.stream.Collectors;
import java.util.Date;

import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.smzq.modular.lifecycle.constant.WebResultStatusConstant;
import vip.xiaonuo.smzq.modular.lifecycle.entity.LifeCycleMineralMea;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.LifeCycleMineralMeaMapper;
import vip.xiaonuo.smzq.modular.lifecycle.po.datamanager.SaveOrUpdateLCMeaPO;
import vip.xiaonuo.smzq.modular.lifecycle.result.ResultWrapper;
import vip.xiaonuo.smzq.modular.lifecycle.result.WebResult;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.LifeCycleMineralMeaService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 于彦飞
 * @since 2024-05-13
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = {"模块"})
@RestController
@RequestMapping("/lifeCycleMineralMea" )

public class LifeCycleMineralMeaController {
    @Autowired
    private LifeCycleMineralMeaService lifeCycleMineralMeaService;
    @Autowired
    private LifeCycleMineralMeaMapper lifeCycleMineralMeaMapper;
    @Resource
    ResultWrapper resultWrapper;

    @ApiOperation(value = "查询所有接口", notes = "查询所有接口")
    @PostMapping(value = "/list")
    public WebResult queryLifeCycleMineralMeaList(@RequestBody LifeCycleMineralMea lifeCycleMineralMea) {

        return resultWrapper.getSuccessWebResult(lifeCycleMineralMeaService.queryLifeCycleMineralMeaList(lifeCycleMineralMea));
    }

    @ApiOperation(value = "分页查询接口", notes = "分页查询接口")
    @PostMapping(value = "/list/page")
    public WebResult queryLifeCycleMineralMeaListPage(@RequestBody LifeCycleMineralMea lifeCycleMineralMea) {

        return resultWrapper.getSuccessWebResult(lifeCycleMineralMeaService.queryLifeCycleMineralMeaListPage(lifeCycleMineralMea));
    }



    @ApiOperation(value = "根据id查询接口", notes = "根据id查询接口")
    @GetMapping(value = "byId/{id}")
    public WebResult queryLifeCycleMineralMeaById(@PathVariable("id") String id) {

        LifeCycleMineralMea entity  = lifeCycleMineralMeaService.getById(id);

        return resultWrapper.getSuccessWebResult(entity);
    }

    @ApiOperation(value = "根据id删除接口", notes = "根据id删除接口")
    @PostMapping(value = "d/{id}")
    public WebResult delById(@PathVariable("id")String id) {
        return resultWrapper.getSuccessWebResult(lifeCycleMineralMeaService.delLifeCycleMineralMea(id));
    }

    @ApiOperation(value = "根据id批量删除接口", notes = "根据id批量删除接口")
    @PostMapping(value = "/d/batch")
    public WebResult delById(@RequestBody List<String> ids) {
        return resultWrapper.getWebResultByDelSuccess(lifeCycleMineralMeaService.delLifeCycleMineralMeaBatch(ids));
    }

    @ApiOperation(value = "新增接口", notes = "新增接口")
    @PostMapping(value = "/save")
    public WebResult save(@RequestBody LifeCycleMineralMea lifeCycleMineralMea) {
        lifeCycleMineralMea.setSaveTime(DateUtil.toLocalDateTime(new Date()));
        boolean isSuccess = lifeCycleMineralMeaService.save(lifeCycleMineralMea);
        if(isSuccess){
            return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.SUCCESS_OPERATION, lifeCycleMineralMea.getId());
        }
        return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.PROCESS_CREATE, null);
    }

    @ApiOperation(value = "批量新增接口", notes = "批量接口")
    @PostMapping(value = "save/batch")
    public WebResult saveBath(@RequestBody List<LifeCycleMineralMea> lifeCycleMineralMeas) {
        lifeCycleMineralMeas.forEach(ele->{
            ele.setSaveTime(DateUtil.toLocalDateTime(new Date()));
        });

        boolean isSuccess = lifeCycleMineralMeaService.saveBatch(lifeCycleMineralMeas);
        if(isSuccess){
            List<String> ids = lifeCycleMineralMeas.stream().map(ele->ele.getId()).collect(Collectors.toList());
            return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.SUCCESS_OPERATION, ids);
        }
        return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.PROCESS_CREATE, null);
    }

    @ApiOperation(value = "更新用户接口", notes = "id必须有值")
    @PostMapping(value = "/update")
    public WebResult update(@RequestBody LifeCycleMineralMea lifeCycleMineralMea) {
        lifeCycleMineralMea.setEditTime(DateUtil.toLocalDateTime(new Date()));
        return resultWrapper.getWebResultByEditSuccess(lifeCycleMineralMeaService.updateById(lifeCycleMineralMea));
    }
    @ApiOperation(value = "批量更新接口", notes = "id必须有值")
    @PostMapping(value = "/update/batch")
    public WebResult updateBatch(@RequestBody List<LifeCycleMineralMea> lifeCycleMineralMeas) {
        lifeCycleMineralMeas.forEach(ele->{
            ele.setEditTime(DateUtil.toLocalDateTime(new Date()));
        });
        return resultWrapper.getWebResultByCreateSuccess(lifeCycleMineralMeaService.updateBatchById(lifeCycleMineralMeas));
    }


    @ApiOperation(value = "新增或者更新接口", notes = "id无值为新增反之则为更新")
    @PostMapping(value = "/saveOrUpdate")
    public WebResult saveOrUpdate(@RequestBody SaveOrUpdateLCMeaPO saveOrUpdateLCMeaPO) {
        saveOrUpdateLCMeaPO.initCb06Only();
        LifeCycleMineralMea lifeCycleMineralMea = BeanUtil.copyProperties(saveOrUpdateLCMeaPO,LifeCycleMineralMea.class);

//        lifeCycleMineralMea.setMineralType(CollUtil.getLast(saveOrUpdateLCMeaPO.getMineralTypes()));

        if(StrUtil.isNotBlank(lifeCycleMineralMea.getId())){
            lifeCycleMineralMea.setEditTime(DateUtil.toLocalDateTime(new Date()));
        }

        if(StrUtil.isBlank(lifeCycleMineralMea.getId())){
            lifeCycleMineralMea.setSaveTime(DateUtil.toLocalDateTime(new Date()));
        }
        return resultWrapper.getSuccessWebResult(lifeCycleMineralMeaService.saveOrUpdate(lifeCycleMineralMea));
    }
    @ApiOperation(value = "批量新增或者更新接口", notes = "id无值为新增反之则为更新")
    @PostMapping(value = "/saveOrUpdate/batch")
    public WebResult saveOrUpdate(@RequestBody List<LifeCycleMineralMea> lifeCycleMineralMeas) {

        lifeCycleMineralMeas.forEach(ele->{
            if(StrUtil.isNotBlank(ele.getId())){
                ele.setSaveTime(DateUtil.toLocalDateTime(new Date()));
            }
        });
        lifeCycleMineralMeas.forEach(ele->{
            if(StrUtil.isNotBlank(ele.getId())){
                ele.setEditTime(DateUtil.toLocalDateTime(new Date()));
            }
        });
        return resultWrapper.getSuccessWebResult(lifeCycleMineralMeaService.saveOrUpdateBatch(lifeCycleMineralMeas));
    }


}
